﻿using ML.Entity;
using ML.IBusiness;
using ML.IRepository;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace ML.Business
{
    /// <summary>
    /// 用户收货地址业务实现类
    /// </summary>
    public class ReceivingAddressBusiness : IReceivingAddressBusiness
    {
        private readonly IReceivingAddressRepository receivingAddressRepository;
        public ReceivingAddressBusiness(IReceivingAddressRepository _receivingAddressRepository)
        {
            receivingAddressRepository = _receivingAddressRepository;
        }

        public BaseResult<IEnumerable<ReceivingAddressEntity>> GetAddRessByUserId(string id)
        {
            var data = receivingAddressRepository.GetAddRessByUserId(id);
            return new BaseResult<IEnumerable<ReceivingAddressEntity>>(200, data);
        }

        public BaseResult<IEnumerable<ReceivingAddressEntity>> GetListByUserId(string id)
        {
            var data = receivingAddressRepository.GetListByUserId(id);
            if (data.Count() <= 0)
            {
                return new BaseResult<IEnumerable<ReceivingAddressEntity>>(202);
            }
            return new BaseResult<IEnumerable<ReceivingAddressEntity>>(200, data);
        }

        public BaseResult<ReceivingAddressEntity> GetDefaulByUserId(string id)
        {
            var data = receivingAddressRepository.GetDefaulByUserId(id);
            if (data == null)
            {
                return new BaseResult<ReceivingAddressEntity>(202);
            }
            return new BaseResult<ReceivingAddressEntity>(200, data);
        }

        public BaseResult<ReceivingAddressEntity> getById(string id)
        {
            var data = receivingAddressRepository.GetById(id);
            if (data == null)
            {
                return new BaseResult<ReceivingAddressEntity>(202);
            }
            return new BaseResult<ReceivingAddressEntity>(200, data);
        }

        public BaseResult<bool> Add(ReceivingAddressEntity receivingAddressEntity)
        {
            //如果选择了默认地址，则需要执行此方法，否则直接添加数据库即可
            bool isTrue = false;
            if (receivingAddressEntity.DefaultMark == (int)DefaultMarkStatus.defaultmark_true)
            {
                isTrue = receivingAddressRepository.AddInfo(receivingAddressEntity);
            }
            else
            {
                isTrue = receivingAddressRepository.Add(receivingAddressEntity);
            }
            if (isTrue)
            {
                return new BaseResult<bool>(200, true);
            }
            return new BaseResult<bool>(201, false);
        }

        public BaseResult<bool> Update(ReceivingAddressEntity receivingAddressEntity)
        {
            //如果选择了默认地址，则需要执行此方法，否则直接添加数据库即可
            bool isTrue = false;
            if (receivingAddressEntity.DefaultMark == (int)DefaultMarkStatus.defaultmark_true)
            {
                isTrue = receivingAddressRepository.UpdateInfo(receivingAddressEntity);
            }
            else
            {
                isTrue = receivingAddressRepository.Update(receivingAddressEntity);
            }
            if (isTrue)
            {
                return new BaseResult<bool>(200, true);
            }
            return new BaseResult<bool>(201, false);
        }

        public BaseResult<bool> Default(string id, string userid)
        {
            var isTrue = receivingAddressRepository.Default(id, userid);
            if (isTrue)
            {
                return new BaseResult<bool>(200, true);
            }
            return new BaseResult<bool>(201, false);
        }

        public BaseResult<bool> Delete(string id)
        {
            var isTrue = receivingAddressRepository.Delete(id);
            if (isTrue)
            {
                return new BaseResult<bool>(200, true);
            }
            return new BaseResult<bool>(201, false);
        }
    }
}